package com.rc.web.service.impl;

import com.google.common.collect.ImmutableMap;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rc.common.constant.Code;
import com.rc.common.constant.CollectType;
import com.rc.common.constant.OrderType;
import com.rc.common.exception.BusinessException;
import com.rc.common.util.RabbitMqHelper;
import com.rc.pojo.dto.activity.ActivityAddDTO;
import com.rc.pojo.dto.activity.ActivityPageQuery;
import com.rc.pojo.dto.activity.UpdateActivityDTO;
import com.rc.pojo.po.Activity;
import com.rc.pojo.po.Area;
import com.rc.pojo.po.Game;
import com.rc.pojo.po.GameOption;
import com.rc.pojo.vo.activity.ActivityDetailVO;
import com.rc.pojo.vo.activity.ActivityListVO;
import com.rc.pojo.vo.activity.ActivityVO;
import com.rc.pojo.vo.activity.PageActivityVO;
import com.rc.web.converter.ActivityConverter;
import com.rc.web.mapper.ActivityMapper;
import com.rc.web.mapper.GameMapper;
import com.rc.web.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 游戏服务实现类
 * 提供游戏相关的业务逻辑实现
 */
@Service
@RequiredArgsConstructor
public class GameServiceImpl extends ServiceImpl<GameMapper, Game> implements GameService {

    /**
     * 根据植物ID获取游戏列表
     * @param plantId 植物ID
     * @return 游戏列表
     */
    @Override
    public List<Game> listByPlantId(Long plantId) {
        // 根据植物ID查询游戏列表
        List<Game> games = mapper.selectByPlantId(plantId);

        // 创建游戏ID与索引的映射
        Map<Long, Integer> idIndexMap = games.stream()
                .collect(Collectors.toMap(Game::getId, games::indexOf));

        // 为每个游戏选项设置下一个游戏的索引
        games.forEach(game -> game.getOptions()
                .forEach(option -> {
                    Long nextId = option.getNextId();
                    Integer nextIndex = idIndexMap.get(nextId);
                    option.setNextIndex(nextIndex);
                }));
        return games;
    }
}
